//
//  TimerTypeSegmentedCell.swift
//  XingCeHelper
//
//  Created by 申铭 on 2022/11/9.
//

import UIKit

let kTimerTypeSegmentedCell: String = "TimerTypeSegmentedCell"

class TimerTypeSegmentedCell: RoundCornerCell {

    @IBOutlet weak var segmentedControl: UISegmentedControl!
    
    var model: TimerTypeSegmentedCellModel? {
        didSet {
            segmentedControl.selectedSegmentIndex = model?.selectedSegmentIndex ?? 0
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        selectionStyle = .none
        segmentedControl.removeAllSegments()
        segmentedControl.insertSegment(withTitle: TimerType.timeSlotTiming.rawValue, at: 0, animated: true)
        segmentedControl.insertSegment(withTitle: TimerType.timer.rawValue, at: 1, animated: true)
        segmentedControl.insertSegment(withTitle: TimerType.countDown.rawValue, at: 2, animated: true)
        segmentedControl.addTarget(self, action: #selector(segmentedControlChange(_ :)), for: .valueChanged)
    }
    
    @objc private func segmentedControlChange(_ segmented: UISegmentedControl) {
        guard let block = model?.segmentedControlResult else { return }
        let index = segmented.selectedSegmentIndex
        block(segmented.titleForSegment(at: index))
    }
    
}
